﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Activities;
using System.Activities.Statements;
using System.Activities.Validation;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WFExtended.Activities;
using WFExtended.Framework.ServiceModel;

namespace WFExtended.Tests.Activities
{
    public abstract class AWcfProxyActivityTest
    {
        protected void WcfStringProxyWithoutInvokerValidationTest<ProxyActivity>()
            where ProxyActivity : Activity, IWcfProxyActivity, new()
        {
            ValidationResults result = ActivityValidationServices.Validate(new ProxyActivity());

            Assert.IsNotNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Contract"));
            Assert.IsNotNull(result.Warnings.SingleOrDefault((E) => E.PropertyName == "Invoker"));
            Assert.IsNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Persistence"));
        }

        protected void WcfMockServiceProxyWithoutInvokerValidationTest<ProxyActivity>()
              where ProxyActivity : Activity, IWcfProxyActivity, new()
        {
            ValidationResults result = ActivityValidationServices.Validate(new ProxyActivity());

            Assert.IsNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Contract"));
            Assert.IsNotNull(result.Warnings.SingleOrDefault((E) => E.PropertyName == "Invoker"));
            Assert.IsNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Persistence"));
        }

        protected void WcfMockServiceProxyWithInvokerValidationTest<ProxyActivity, Contract>()
            where Contract : class
             where ProxyActivity : AWcfProxyActivity<Contract>, IWcfProxyActivity, new()
        {
            DelegateInArgument<WcfProxy<Contract>> proxy = new DelegateInArgument<WcfProxy<Contract>>();

            ValidationResults result = ActivityValidationServices.Validate(new ProxyActivity()
            {
                Body = new System.Activities.ActivityAction<Framework.ServiceModel.WcfProxy<Contract>>()
                {
                    Argument = proxy,
                    Handler = new WcfOperationInvoker()
                    {
                        OperationName = "Ping",
                        Proxy = proxy
                    }
                }
            });

            Assert.IsNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Contract"));
            Assert.IsNull(result.Warnings.SingleOrDefault((E) => E.PropertyName == "Invoker"));
            Assert.IsNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Persistence"));
        }

        protected void WcfMockServiceProxyWithGenericInvokerValidationTest<ProxyActivity, Contract>()
            where Contract : class
            where ProxyActivity : AWcfProxyActivity<Contract>, IWcfProxyActivity, new()
        {
            DelegateInArgument<WcfProxy<Contract>> proxy = new DelegateInArgument<WcfProxy<Contract>>();

            ValidationResults result = ActivityValidationServices.Validate(new ProxyActivity()
            {
                Body = new System.Activities.ActivityAction<Framework.ServiceModel.WcfProxy<Contract>>()
                {
                    Argument = proxy,
                    Handler = new WcfOperationInvoker<bool>()
                    {
                        OperationName = "CheckData",
                        Proxy = proxy
                    }
                }
            });

            Assert.IsNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Contract"));
            Assert.IsNull(result.Warnings.SingleOrDefault((E) => E.PropertyName == "Invoker"));
            Assert.IsNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Persistence"));
        }

        protected void WcfMockServiceNoPersistenceProxyValidationTest<ProxyActivity, Contract>()
            where Contract : class
            where ProxyActivity : AWcfProxyActivity<Contract>, IWcfProxyActivity, new()
        {
            var workflow = new ProxyActivity()
            {
            };
            ValidationResults result = ActivityValidationServices.Validate(workflow);

            Assert.IsNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Persistence"));
            Assert.IsTrue(workflow.EnablePersistence);

            result = ActivityValidationServices.Validate(new ProxyActivity()
            {
                EnablePersistence = false
            });

            Assert.IsNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Persistence"));

            result = ActivityValidationServices.Validate(new ProxyActivity()
            {
                EnablePersistence = false,
                Body = new ActivityAction<WcfProxy<Contract>>()
                {
                    Handler = new Persist()
                    {
                    }
                }

            });

            Assert.IsNotNull(result.Errors.SingleOrDefault((E) => E.PropertyName == "Persistence"));
        }

    }
}
